import os
import warnings

warnings.filterwarnings("ignore")
import pandas as pd

path = 'D:/Projects/PycharmProjects/pythonProject/pandasLearning/taidibei/task3\DataA'
dirD = {}
for i in os.listdir(path):
    dirD[i] = 0

data2 = pd.read_excel("criteria3.xlsx")

lst = []


def score(p):
    for j in os.listdir(path + '/' + p):
        if j == 'task3.xlsx':
            print(p)
            lst.append(calc_error(p))


def calc_error(s):
    data1 = pd.read_excel(path + '/' + s + "/task3.xlsx")
    data1.dropna(axis="index", how='all', inplace=True)
    data1.dropna(axis="columns", how='all', inplace=True)
    columns = list(data1.columns)
    columns[0] = "企业名称"
    data1.columns = columns
    data_1 = pd.merge(left=data1, right=data2, on="企业名称", how="left")
    data_1_ = pd.merge(left=data1, right=data2, on="企业名称", how="left")
    data_1_.dropna(axis="index", how='any', inplace=True)
    data_2 = pd.merge(left=data1, right=data2, on="企业名称", how="right")
    data_2_ = pd.merge(left=data1, right=data2, on="企业名称", how="right")
    data_2_.dropna(axis="index", how='any', inplace=True)
    return s, data_1["企业名称"].count() - data_1_["企业名称"].count(), \
           data_2["企业名称"].count() - data_2_["企业名称"].count(),


error_list = []
for i in os.listdir(path):
    score(i)
df = pd.DataFrame(lst, columns=['作品号', '错误1', '错误2'])
df['错误总数'] = df['错误1'] + df['错误2']
df.loc[df['错误总数'] >= 31, '得分'] = 0
df.loc[(df['错误总数'] <= 30) & (df['错误总数'] >= 16), '得分'] = 5
df.loc[(df['错误总数'] <= 15) & (df['错误总数'] >= 6), '得分'] = 10
df.loc[(df['错误总数'] <= 5), '得分'] = 15
df.to_excel("summary/result3_2.xlsx", index=False)
